package com.ljkj.cordial.chat.dao;

import com.lanjiu.lib.utils.json.JsonUtils;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.lang.reflect.ParameterizedType;

/**
 * 嵌套table辅助
 *
 * @param <T>
 */
public class DbConverter<T> implements PropertyConverter<T, String> {
    @Override
    public T convertToEntityProperty(String databaseValue) {
        if (databaseValue == null) {
            return null;
        }
        Class<T> rawType = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return JsonUtils.fromJson(databaseValue, rawType);
    }

    @Override
    public String convertToDatabaseValue(T entityProperty) {
        if (entityProperty == null) {
            return null;
        }
        return JsonUtils.toJson(entityProperty);
    }
}
